<?php
global $current_user;
if ((!current_user_can('manage_options')) || ($current_user->user_login != 'admin'))
{
	wp_die('You do not have sufficient permissions to access this page.');
}
$date = $_GET['date'];
if($date == '')
    $today = date('Y-m-d', time() + 25200);
else
    $today = $date;
$st_have_cp = cpx_today_store_have_cp_published($date);
$last_send = get_option('last_send_email_newcp');
// Get emails from wp_emails
global $wpdb;
$qr = "SELECT * FROM wp_emails ORDER BY ID DESC";
$qr2 = "SELECT * FROM wp_emails WHERE status = 0 ORDER BY ID DESC";
$rs_email_notsend = $wpdb->get_results($qr2);
$rs_emails_sended = $wpdb->get_results($qr);
 ?>
<style type="text/css">
.email_schedule{border: 1px solid #ccc;margin: 10px;padding: 10px;background: white;}
#em_schedule .title{color:grey;}
#em_schedule .stname{font-size: 18px;}
#wrap_email_schedule .message{font-size: 11px;background-color: yellow;padding: 5px;}
table.ex{border: 1px solid;text-align: left;}
table.ex tr td{border: 1px solid #BBB;text-align: center;}
table.ex #site{background: whitesmoke;}
.em_head{font-weight: bold;}
</style>
<div class="email_schedule" id="em_schedule">
<h3>Stores have coupons published <?php echo $today; ?> ( <?php echo count($st_have_cp)?> ): </h3>
<?php
foreach ($st_have_cp as $s) {
    $st_id = $s[0];
    $st_title = get_the_title($st_id);
    $viewcp_url = admin_url()."edit.php?s&post_status=all&post_type=coupon&action=-1&m=0&ADMIN_FILTER_FIELD_VALUE={$st_id}&paged=1&mode=list&action2=-1";
    ?>
    <a class="stname" title="View <?php echo $st_title; ?> coupons" href="<?php echo $viewcp_url; ?>"><?php echo $st_title; ?></a> |
<?php
} ?>
</div>
<div class="email_schedule" id="wrap_email_schedule">
    Last send : <?php echo get_option('last_send_email_newcp'); ?> <?php if($last_send == $today) echo "( today )"; ?> <br />
    <input type="text" name="date" id="date" value="<?php echo $today; ?>" /><a href="#" id="reload" style="visibility: hidden">reload</a><br />
    <a id="prepare_send" style="cursor: pointer;" >Prepare send</a> |
    <a id="run_send_email" style="cursor: pointer;" >Send emails to subscribers</a>
    <label class="message" id="status_mess" name="status_mess" ></label>
</div>
<?php if(count($rs_emails_sended) > 0): ?>
<div class="email_schedule">
<h1 id="toggle" style="cursor: pointer;">Emails Sended</h1>
<span>
<?php echo "Total ".count($rs_emails_sended)." | Not send ".count($rs_email_notsend)." | Sended ".(count($rs_emails_sended)-count($rs_email_notsend));?>
</span>
    <table class="form-table ex" id="list_sended" >
        <tr valign="top" class="em_head">
            <td>ID</td>
            <td>To</td>
            <td>Coupon IDs</td>
            <td>Store</td>
            <td>Follow ID</td>
            <td>Status</td>
            <td>Send time</td>
        </tr>
        <?php foreach($rs_emails_sended as $v): ?>
        <tr>
            <td><?php echo $v->ID;?></td>
            <td><?php echo $v->em_to;?></td>
            <td><textarea><?php echo $v->cp_ids;?></textarea></td>
            <td><a target="_blank" href="<?php echo home_url("/?p={$v->st_id}");?>"><?php echo get_the_title($v->st_id);?></a></td>
            <td><?php echo $v->follow_id;?></td>
            <?php if($v->status == 0): ?>
            <td style="background: red;color:white">
            <?php else: ?>
            <td style="background: lightgreen;color:white">
            <?php endif; ?>
            <?php echo ($v->status == 0?'Yet':'Sended');?>
            </td>

            <td><?php echo $v->send_time;?></td>
        </tr>
        <?php endforeach; ?>
    </table>
</div>
<?php endif; ?>